home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / clipper / nannws14.zip / LOCKS.PRG < prev    next >
Text File  |  1986-10-31  |  3KB  |  164 lines

  1. ****
  2. *  Locks.prg
  3. *
  4. *  Assumes SET EXCLUSIVE OFF
  5. *
  6. *   UDF's for:
  7. *   Command               Purpose
  8. *   -----------------------    ------------------------
  9. *   1. USE <file> [EXCLUSIVE]    Share/Deny others USE
  10. *   2. FLOCK()            Lock current file
  11. *   3. RLOCK()/LOCK()        Lock current record
  12. *   4. APPEND BLANK        Lock new blank record
  13.  
  14.  
  15. ****
  16. *  NET_USE function
  17. *
  18. *  Trys to open a file for exclusive or shared use.
  19. *  SET INDEXes in calling procedure if successful.
  20. *  Pass the following parameters
  21. *    1. Character - name of the .DBF file to open
  22. *    2. Logical - mode of open (exclusive/.NOT. exclusive)
  23. *    3. Numeric - seconds to wait (0 = wait forever)
  24. *
  25. *  Example:
  26. *    IF NET_USE("Accounts", .T., 5)
  27. *       SET INDEX TO Name
  28. *    ELSE
  29. *       ? "Account file not available"
  30. *    ENDIF
  31.  
  32. FUNCTION NET_USE
  33. PARAMETERS file, ex_use, wait
  34. PRIVATE forever
  35.  
  36. forever = (wait = 0)
  37. DO WHILE (forever .OR. wait > 0)
  38.  
  39.    IF ex_use                    && exclusive
  40.       USE &file EXCLUSIVE
  41.    ELSE
  42.       USE &file           && shared
  43.    ENDIF
  44.  
  45.    IF .NOT. NETERR()           && USE succeeds
  46.       RETURN (.T.)
  47.    ENDIF
  48.    
  49.    INKEY(1)                   && wait 1 second
  50.    wait = wait - 1
  51. ENDDO
  52. RETURN (.F.)            && USE fails
  53. * End - NET_USE
  54.  
  55.  
  56. ****
  57. *  FIL_LOCK function
  58. *
  59. *  Trys to lock the current shared file 
  60. *  Pass the following parameter
  61. *    1. Numeric - seconds to wait (0 = wait forever)
  62. *
  63. * Example:
  64. *    IF FIL_LOCK(5)
  65. *      REPLACE ALL Price WITH Price * 1.1
  66. *    ELSE
  67. *      ? "File not available"
  68. *    ENDIF 
  69.  
  70. FUNCTION FIL_LOCK
  71. PARAMETERS wait
  72. PRIVATE forever
  73.  
  74. IF FLOCK()
  75.    RETURN (.T.)        && locked
  76. ENDIF
  77.       
  78. forever = (wait = 0)
  79. DO WHILE (forever .OR. wait > 0)
  80.  
  81.    INKEY(.5)            && wait 1/2 second
  82.    wait = wait - .5
  83.  
  84.    IF FLOCK()
  85.       RETURN (.T.)        && locked
  86.    ENDIF
  87.       
  88. ENDDO
  89. RETURN (.F.)            && not locked
  90. * End - FIL_LOCK
  91.  
  92.  
  93. ****
  94. *  REC_LOCK function
  95. *
  96. *  Trys to lock the current record
  97. *  Pass the following parameter
  98. *    1. Numeric - seconds to wait (0 = wait forever)
  99. *
  100. * Example:
  101. *   IF REC_LOCK(5)
  102. *      REPCLACE Price WITH newprice
  103. *   ELSE
  104. *      ? "Record not available"
  105. *   ENDIF  
  106.  
  107. FUNCTION REC_LOCK 
  108. PARAMETERS wait
  109. PRIVATE forever
  110.  
  111. IF RLOCK()
  112.    RETURN (.T.)        && locked
  113. ENDIF
  114.  
  115. forever = (wait = 0)
  116. DO WHILE (forever .OR. wait > 0)
  117.  
  118.    IF RLOCK()
  119.       RETURN (.T.)        && locked
  120.    ENDIF
  121.       
  122.    INKEY(.5)            && wait 1/2 second
  123.    wait = wait - .5
  124.  
  125. ENDDO
  126. RETURN (.F.)            && not locked
  127. * End - REC_LOCK
  128.  
  129.  
  130. ****
  131. *   ADD_REC function
  132. *
  133. *  Returns true if record appended.  The new record is current
  134. *  and locked.
  135. *  Pass the following parameter
  136. *    1. Numeric - seconds to wait (0 = wait forever)
  137. *
  138.  
  139. FUNCTION ADD_REC
  140. PARAMETERS wait
  141. PRIVATE forever
  142.  
  143. APPEND BLANK
  144. IF .NOT. NETERR()
  145.    RETURN (.T.)
  146. ENDIF
  147.  
  148. forever = (wait = 0)
  149. DO WHILE (forever .OR. wait > 0)
  150.  
  151.    APPEND BLANK
  152.    IF .NOT. NETERR()
  153.       RETURN .T.
  154.    ENDIF
  155.       
  156.    INKEY(.5)            && wait 1/2 second
  157.    wait = wait - .5
  158.  
  159. ENDDO
  160. RETURN (.F.)            && not locked
  161. * End ADD_REC
  162.  
  163. * EOF - Locks.prg
  164.